package com.yc.videoview;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilityLifecycleCallbacks;
import ohos.app.Context;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventSubscribeInfo;
import ohos.event.commonevent.CommonEventSubscriber;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.utils.PacMap;

import java.util.logging.Handler;

/**
 * 用于控制悬浮窗显示周期
 * 使用了三种方法针对返回桌面时隐藏悬浮按钮
 * 1.startCount计数，针对back到桌面可以及时隐藏
 * 2.监听home键，从而及时隐藏
 * 3.resumeCount计时，针对一些只执行onPause不执行onStop的奇葩情况
 */
public class FloatLifecycle extends CommonEventSubscriber implements AbilityLifecycleCallbacks {
    private static final String SYSTEM_DIALOG_REASON_KEY = "reason";
    private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
    private static final long delay = 300;
    private EventHandler eventHandler;
    private Class[] activities;
    private boolean showFlag;
    private int startCount;
    private int resumeCount;
    private boolean appBackground;
    private LifecycleListener mLifecycleListener;

    FloatLifecycle(Context applicationContext, boolean showFlag, Class[] activities, LifecycleListener lifecycleListener, CommonEventSubscribeInfo subscribeInfo) {
        super(subscribeInfo);
        this.showFlag = showFlag;
        this.activities = activities;
        mLifecycleListener = lifecycleListener;
        eventHandler = new EventHandler(EventRunner.create(true));

      /*
        ((Application) applicationContext).registerActivityLifecycleCallbacks(this);
        applicationContext.registerReceiver(this, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));*/
    }

    private boolean needShow(Ability activity) {
        if (activities == null) {
            return true;
        }
        for (Class a : activities) {
            if (a.isInstance(activity)) {
                return showFlag;
            }
        }
        return !showFlag;
    }

    @Override
    public void onAbilityStart(Ability ability) {
        startCount++;
    }

    @Override
    public void onAbilityActive(Ability ability) {
        resumeCount++;
        if (needShow(ability)) {
            mLifecycleListener.onShow();
        }
        if (appBackground) {
            appBackground = false;
        }
    }

    @Override
    public void onAbilityInactive(Ability ability) {
        resumeCount--;
        eventHandler.postTask(new Runnable() {
            @Override
            public void run() {
                if (resumeCount == 0) {
                    appBackground = true;
                }
            }
        }, delay);
    }

    @Override
    public void onAbilityForeground(Ability ability) {
    }

    @Override
    public void onAbilityBackground(Ability ability) {
    }

    @Override
    public void onAbilityStop(Ability ability) {
        startCount--;
    }

    @Override
    public void onAbilitySaveState(PacMap pacMap) {
    }

    @Override
    public void onReceiveEvent(CommonEventData commonEventData) {
    }
}
